﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using fbasync.webcomponents;
using System.Collections.Generic;

namespace fbasync.web
{
    public partial class AlbumBrowse : FacebookPage
    {
        private IList<Album> _albums = null;
        private IList<Photo> _albumCovers = null;
        private long _viewingUid;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            Connection.GetAlbumsCompleted += new EventHandler<AsyncCompletedEventArgs<System.Collections.Generic.IList<Album>>>(Connection_GetAlbumsCompleted);
            Connection.GetPhotosCompleted += new EventHandler<AsyncCompletedEventArgs<System.Collections.Generic.IList<Photo>>>(Connection_GetPhotosCompleted);
            Connection.GetUsersCompleted += new EventHandler<AsyncCompletedEventArgs<IList<User>>>(Connection_GetUsersCompleted);


            _viewingUid = Connection.UserId.Value;
            if (null != Request.Params["owner"])
                _viewingUid = long.Parse(Request.Params["owner"]);

            Connection.GetAlbumsAsync("owner=" + _viewingUid.ToString());
            Connection.GetPhotosAsync("pid in (select cover_pid from album where owner=" + _viewingUid.ToString() + ")");
            Connection.GetUsersAsync(string.Concat("uid in (select uid2 from friend where uid1=", Connection.UserId.Value.ToString(), ") or uid=", Connection.UserId.Value.ToString()));
        }

        void Connection_GetUsersCompleted(object sender, AsyncCompletedEventArgs<IList<User>> e)
        {
            Connection.GetUsersCompleted -= new EventHandler<AsyncCompletedEventArgs<IList<User>>>(Connection_GetUsersCompleted);

            //TODO: error handling
            lvFriends.DataSource = e.Result.OrderBy(u => u.uid != Connection.UserId);
            lvFriends.DataBind();

            friendName.Controls.Add(new LiteralControl(e.Result.Single(u => u.uid == _viewingUid).name));
        }

        void Connection_GetAlbumsCompleted(object sender, AsyncCompletedEventArgs<System.Collections.Generic.IList<Album>> e)
        {
            Connection.GetAlbumsCompleted -= new EventHandler<AsyncCompletedEventArgs<System.Collections.Generic.IList<Album>>>(Connection_GetAlbumsCompleted);
            //TODO: error handling
            _albums = e.Result;
        }

        void Connection_GetPhotosCompleted(object sender, AsyncCompletedEventArgs<System.Collections.Generic.IList<Photo>> e)
        {
            Connection.GetPhotosCompleted -= new EventHandler<AsyncCompletedEventArgs<System.Collections.Generic.IList<Photo>>>(Connection_GetPhotosCompleted);
            //TODO: error handling
            _albumCovers = e.Result;
        }

        protected override void OnPreRenderComplete(EventArgs e)
        {
            base.OnPreRenderComplete(e);

            //we have all our state now (all async operations are complete).
            //bind the album control.
            lvAlbums.DataSource = _albums;
            lvAlbums.DataBind();
        }

        protected Photo GetCoverPhoto(Album a)
        {
            return _albumCovers.Single(p => p.pid == a.cover_pid);
        }
    }
}
